<template>
  <div class="goods-info-container" v-loading.lock.lock="loading">
    <!-- 商品库存数量、商品已售数量、商品是否库存紧张 -->
    <div class="goods-num-info">
      <div class="">商品总数</div>
      <div class="">订单总数</div>
      <div class="">销售总额</div>
      <div class="">已售商品数量</div>
      <div class="">今日订单总数</div>
      <div class="">今日销售额</div>
    </div>
    <!-- 商品信息表 -->
    <div class="goods-info-table">
      <div class="title">商品热销榜</div>
      <div class="table-content">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" :index="indexMethod" />
          <el-table-column prop="date" label="Date" width="180" />
          <el-table-column prop="name" label="Name" width="180" />
          <el-table-column prop="address" label="Address" />
        </el-table>
      </div>
    </div>
    <!-- 商品数据可视化图形 -->
    <div class="goods-visualization">
      <div class="title">订单统计</div>
      <div class="order-content">
        <div class="order-data"></div>
        <div class="data-show" ref="main"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import * as echarts from 'echarts'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { ref, reactive, onMounted } from 'vue'

let loading = ref(false)
const main = ref(null)
const router = useRouter()
let fullscreenLoading = ref(false)
const option = reactive({
  title: {
    text: 'Stacked Area Chart'
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
      label: {
        backgroundColor: '#6a7985'
      }
    }
  },
  legend: {
    data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
  },
  toolbox: {
    feature: {
      saveAsImage: {}
    }
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: [
    {
      type: 'category',
      boundaryGap: false,
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    }
  ],
  yAxis: [
    {
      type: 'value'
    }
  ],
  series: [
    {
      name: 'Email',
      type: 'line',
      stack: 'Total',
      areaStyle: {},
      emphasis: {
        focus: 'series'
      },
      data: [120, 132, 101, 134, 90, 230, 210]
    },
    {
      name: 'Union Ads',
      type: 'line',
      stack: 'Total',
      areaStyle: {},
      emphasis: {
        focus: 'series'
      },
      data: [220, 182, 191, 234, 290, 330, 310]
    },
    {
      name: 'Video Ads',
      type: 'line',
      stack: 'Total',
      areaStyle: {},
      emphasis: {
        focus: 'series'
      },
      data: [150, 232, 201, 154, 190, 330, 410]
    },
    {
      name: 'Direct',
      type: 'line',
      stack: 'Total',
      areaStyle: {},
      emphasis: {
        focus: 'series'
      },
      data: [320, 332, 301, 334, 390, 330, 320]
    },
    {
      name: 'Search Engine',
      type: 'line',
      stack: 'Total',
      label: {
        show: true,
        position: 'top'
      },
      areaStyle: {},
      emphasis: {
        focus: 'series'
      },
      data: [820, 932, 901, 934, 1290, 1330, 1320]
    }
  ]
})

// 创建图标
const createImageTable = () => {
  let myChart
  if (myChart) {
    myChart.dispose()
  }
  myChart = echarts.init(main.value)
  option && myChart.setOption(option)
  // 自适应品目大小
  window.onresize = () => {
    myChart.resize()
  }
}
// 获取商品信息
const getGoodsInfo = async () => {
  fullscreenLoading.value = true
  try {
    ElMessage({
      type: 'error',
      message: '请求失败！'
    })
  } catch {
    const data = await axios.get('/bus/goods/' + id)
    if (data.code === 0) {
      // 获取商品信息成功
      ElMessage({
        type: 'success',
        message: data.msg
      })
    } else if (data.code === 403 || data.msg === '身份验证失败') {
      ElMessage({
        type: 'error',
        message: data.msg
      })
      localStorage.removeItem('bus_token')
      router.replace('/login')
    } else {
      ElMessage({
        type: 'error',
        message: data.msg
      })
    }
  } finally {
    fullscreenLoading.value = false
  }
}

const indexMethod = (index) => {
  return index * 2
}
const tableData = [
  {
    date: '2016-05-03',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tag: 'Home'
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tag: 'Office'
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tag: 'Home'
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tag: 'Office'
  }
]
onMounted(() => createImageTable())
</script>

<style lang="scss" scoped>
.goods-info-container {
  padding: 50px;
  min-width: 980px;

  .goods-num-info {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;

    div {
      width: 30%;
      padding: 15px;
      color: #909399;
      margin-bottom: 10px;
      border: 1px solid #e1e1e1;
    }
  }
  .goods-info-table {
    border: 1px solid #e1e1e1;
    margin-top: 30px;
    border-bottom-width: 0;

    .title {
      padding: 15px;
      background-color: #f2f6fc;
      font-weight: 700;
      color: #606060;
    }
  }
  .goods-visualization {
    border: 1px solid #e1e1e1;
    margin-top: 30px;

    .title {
      padding: 15px;
      background-color: #f2f6fc;
      font-weight: 700;
      color: #606060;
    }

    .order-content {
      display: flex;
      height: 500px;

      .order-data {
        flex: 15;
        background-color: #f1e2c7;
      }
      .data-show {
        flex: 85;
      }
    }
  }
}
</style>
